package com.zbbt.sportslottery.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.gc.materialdesign.views.ButtonRectangle;
import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.net.TypeResultHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.widget.ClearEditText;
import com.zbbt.sportslottery.util.Md5;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.ToastUtil;
/**
 * 
 * @ClassName: ModifiPwdActivity
 * @Description: 修改密码
 * @author: yangwenquan
 * @date: 2015年3月3日 下午4:34:53     
 * @version: V1.0
 */
public class ModifiPwdActivity extends SimpleBaseActivity{
	private ClearEditText et_pswd_new;
	private ClearEditText et_pswd_old;
	private ButtonRectangle bt_submit;
	private ProgressBarCircularIndetermininate mProgressBar;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
            setContentView(R.layout.activity_change_psd);
	}

	@Override
	protected void initView() {
		mProgressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.mProgressBar);
		bt_submit = (ButtonRectangle) findViewById(R.id.bt_submit);
		et_pswd_old = (ClearEditText) findViewById(R.id.et_pswd_old);
		et_pswd_new = (ClearEditText) findViewById(R.id.et_pswd_new);
		bt_submit.setTextSize(20);
		bt_submit.setTextColor(getResources().getColor(R.color.white));
		
	}

	@Override
	protected void setListener() {
		bt_submit.setOnClickListener(this);
		
	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.bt_submit:
			String oldPswd = et_pswd_old.getText().toString();
			String newPswd = et_pswd_new.getText().toString();
			if (TextUtils.isEmpty(oldPswd)) {
				ToastUtil.showShort("请输入旧密码");
				et_pswd_old.setShakeAnimation();
				return;
			}
			if (oldPswd.length() > 16 || oldPswd.length() < 6) {
				ToastUtil.showLong("您输入的旧密码应在6~16个字符");
				et_pswd_old.setShakeAnimation();
				return;
			}
			if (TextUtils.isEmpty(newPswd)) {
				ToastUtil.showShort("请输入新密码");
				et_pswd_new.setShakeAnimation();
				return;
			}

			if (newPswd.length() > 16 || newPswd.length() < 6) {
				ToastUtil.showLong("您输入的新密码应在6~16个字符");
				et_pswd_new.setShakeAnimation();
				return;
			}
			httpData(oldPswd, newPswd);
			break;

		default:
			break;
		}

	}

	private void httpData(String oldPswd, final String newPswd) {
		JsonObject jsonObject = new JsonObject();
		String userName = SharePreferUtil.getString(Constant.USER_NAME, null);
		String oldPswdMD5 = Md5.getMD5(oldPswd);
		String newPswdMD5 = Md5.getMD5(newPswd);
		jsonObject.addProperty("mobile", userName);
		jsonObject.addProperty("oldpassword", oldPswdMD5);
		jsonObject.addProperty("newpassword", newPswdMD5);
		mProgressBar.setVisibility(View.VISIBLE);
		ZbHttpClient.postNewApi(ServerConfig.API_MODIFYPWD, jsonObject, new TypeResultHttpHandler(true) {

			@Override
			public void onSuccess() {
				mProgressBar.setVisibility(View.GONE);
				SharePreferUtil.putString(Constant.PASSWORD, newPswd);
				App.isLogin = false;
				ToastUtil.showShort("密码修改成功,请重新登录");
				goActivity(LoginActivity.class);
			}


			@Override
			public void onFailure(String errorCode, String errorMsg) {
				mProgressBar.setVisibility(View.GONE);
				
			}
		});
	}

	@Override
	protected void initData() {
		
	}

}
